<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/22 0022
 * Time: 13:53
 */

namespace app\admin\controller;

use think\Request;


class Member extends Base
{
    protected  $_salesmanModel;

    protected  $_ipModel;

    public function __construct(Request $request = null){

        parent::__construct($request);

        $this->_salesmanModel = new  \app\model\model\Salesman();

        $this->_ipModel = new  \app\model\model\Accessip();
    }

    public function salesman(){
        if(Request::instance()->post()) {
            $where = [];
            $name = input('name');
            if($name){
                $where['name'] = ['like',"%$name%"];
            }
            $data = $this->_salesmanModel->getList($where, input('limit'));

            $list = $data->toArray();
            echo json_echo($list);
        } else {
            return $this->fetch();
        }
    }

    public function addsalesman(){
        if(Request::instance()->post()) {
            $params = [
                'name'          => input('name'),
                'mobile'        => input('mobile'),
                'password'      => md5(input('password')),
                'email'         => input('email'),
                'fax'           => input('fax'),
                'telephone'     => input('telephone'),
                'branch'        => input('branch'),
                'status'        => input('status'),
                'create_time'   => time()
            ];
            $res = $this->_salesmanModel->addOne($params);
            if($res){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        } else {
            return $this->fetch();
        }
    }

    public function editsalesman(){
        if(Request::instance()->post()) {
            $params = [
                'name'          => input('name'),
                'mobile'        => input('mobile'),
                'email'         => input('email'),
                'fax'           => input('fax'),
                'telephone'     => input('telephone'),
                'branch'        => input('branch'),
                'status'        => input('status')
            ];
            if(input('password')){
                $params['password'] =  md5(input('password'));
            }
            $res = $this->_salesmanModel->updateOne(input('id'), $params);
            if($res === false){
                return json_echo_msg(500,'编辑失败');
            }else{
                return json_echo_msg(200,'编辑成功');
            }
        } else {
            $data = $this->_salesmanModel->getOneById(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    public function delsalesman(){
        $res = $this->_salesmanModel->del(input('id'));
        if($res == true){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    public function delallsalesman(){
        $re = $this->_salesmanModel->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(500,'删除失败！');
        }
    }
}